package com.lastproject.web.bean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ArrayDataModel;
import javax.faces.model.DataModel;

import com.lastproject.dto.SurveyDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.SurveyDAORemote;
import com.lastproject.ejb.dao.UserDAORemote;

@ManagedBean
@RequestScoped
public class SurveyListForUserTableData {

	private DataModel<SurveyListForUserBean> filterModel = null;

	@ManagedProperty(value = "#{surveyListForUserBean}")
	private SurveyListForUserBean survListForUser; // cb

	private SurveyListForUserBean[] surveysList; // customers

	@EJB
	private SurveyDAORemote survDAOr;

	@EJB
	private UserDAORemote userDAOr;

	@PostConstruct
	public void postConstr() {
		
		FacesContext f = FacesContext.getCurrentInstance();
		UserDTO userDTO = (UserDTO) f.getExternalContext().getSessionMap().get("userDTO");

		List<SurveyDTO> sList = null;
		try {
			sList = survDAOr.getSurveyListByUserId(userDTO.getId());

			// System.out.println("Test: " + cList.toString());
			SurveyDTO[] sdtoArr = new SurveyDTO[sList.size()];

			sList.toArray(sdtoArr);

			surveysList = new SurveyListForUserBean[sList.size()];
			// System.out.println("Test de lista: " + surveysList.toString());

			for (int i = 0; i < sdtoArr.length; i++) {
				// System.out.println("Element DTO: " + sdtoArr[i].getTitle());
				surveysList[i] = new SurveyListForUserBean(sdtoArr[i]);
				// System.out.println("Element bean: " + sdtoArr[i].getTitle());
			}
		} catch (NullPointerException e) {

		}
		filterModel = new SurveyListForUserSortFilterModel(
				new ArrayDataModel<SurveyListForUserBean>(surveysList));
		// filterModel =
		// new ArrayDataModel<SurveyListForUserBean>(surveysList);

		// int count = 0;
		// for (CustomerDTO c : cdtoArr) {
		// customers[count] = new CustomerBean(c);
		// count++;
		// }
	}

	public SurveyListForUserTableData() {

	}

	public SurveyListForUserBean getSurvListForUser() {
		return survListForUser;
	}

	public void setSurvListForUser(SurveyListForUserBean survListForUser) {
		this.survListForUser = survListForUser;
	}

	public DataModel<SurveyListForUserBean> getSurveysList() {
		return filterModel;
	}

}
